﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iBank.Logica;
using System.Threading;

namespace iBank.Interfaz
{
    public partial class VentanaPrincipal : Form
    {
        //Tiempo de espera del splash
        Thread espera;

        //Hilo del servidor
        Servidor serv = new Servidor();
        Thread hiloServidor;

        public VentanaPrincipal()
        {
            InitializeComponent();
            Hide();
        }

        private void VentanaPrincipal_Load(object sender, EventArgs e)
        {
            //Se inicia el hilo de espera del splash
            espera = new Thread(esperando);
            espera.IsBackground = true;
            espera.Start();

            //Se inicia el hilo de coneccion del servidor
            hiloServidor = new Thread(serv.Start);
            hiloServidor.IsBackground = true;
            hiloServidor.Start();
            toolEstado.Name = "Estado: ON";
        }

        private void salirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void esperando() {
            Console.WriteLine("");
            Thread.Sleep(8000);
        }

        private void VentanaPrincipal_Deactivate(object sender, EventArgs e)
        {
            if (!espera.IsAlive)
            {
                progressBar.Hide();
                labelConectando.Hide();
                this.Hide();
            }
        }

        private void acercaDeIBankToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (toolDesconectar.Text.Equals("Desconectar"))
            {
                hiloServidor.Suspend();
                toolEstado.Text = "Estado: OFF";
                toolDesconectar.Text = "Conectar";
            }
            else
            {
                hiloServidor.Resume();
                toolEstado.Text = "Estado: ON";
                toolDesconectar.Text = "Desconectar";
            }
        }
    }
}
